﻿using System;
using System.Collections.Generic;
using System.Linq;
using VoiceAd.Models;

namespace VoiceAd.DAL
{
    public class ServicesRepository: IServicesRepository, IDisposable
    {
        private dbVoiceAdDataContext context = new dbVoiceAdDataContext();

        public ServicesRepository(dbVoiceAdDataContext db)
		{
			this.context = db;
		}

		public IEnumerable<tblServices>GetListService()
		{
			return context.tblServices.ToList();
		}

        public tblServices GetServiceById(int id)
		{
            return context.tblServices.Where(x => x.Id == id).FirstOrDefault();
		}

        public void InsService(tblServices tblservices)
		{
            context.tblServices.InsertOnSubmit(tblservices);
			context.SubmitChanges();
		}
                
        public void UpdService(tblServices tblservice)
		{
            tblServices obj = context.tblServices.Where(x => x.Id == tblservice.Id).First();
            obj.Title_Vn = tblservice.Title_Vn;
            obj.Title_En = tblservice.Title_En;
            obj.Body_Vn = tblservice.Body_Vn;
            obj.Body_En = tblservice.Body_En;            
			context.SubmitChanges();
		}

        public void DelService(int id)
		{
			tblServices obj =  context.tblServices.Single(x => x.Id == id);
			context.tblServices.DeleteOnSubmit(obj);
			context.SubmitChanges();
		}

		private bool disposed = false;

		protected virtual void Dispose(bool disposing)
		{
			if (!this.disposed)
			{
				if (disposing)
				{
					context.Dispose();
				}
			}
			this.disposed = true;
		}

		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}
    }
}